/**
 * Created by Administrator on 2016/5/3.
 */
var SPSdk = (function () {
    var LOGIN_TOKEN = "http://passport.h5.1862.cn/open/access_token";
    var LOGIN_USER = "http://passport.h5.1862.cn/open/user";

    var PAY_ORDER = "http://passport.h5.1862.cn/pay/apply";
    var PAY_SHOW = "http://passport.h5.1862.cn/payment/list";

    var BTN_URL = "http://ttgc.1862.cn/resources/js/returnPreviousPageBtn.js";
    function SPSdk() {
        this.appId = "";
        this.appKey = "";
        this.token = "";
        this.payKey = "";
        this.loginKey = "";
        this.queryParams = {};
        this.userToken = "";
        this.otherKey = "";
        this.userID = "";
    }

    var __proto = SPSdk.prototype;

    __proto.login = function (param, callback, shareHandler) {
        var m_this = this;
        console.log("壕鑫网络登录：" + JSON.stringify(param));
        LayaCommon.getPostJson(encodeURIComponent(LOGIN_TOKEN),encodeURIComponent("partner_id="+m_this.appId+"&secret="+m_this.appKey+"&code="+m_this.queryParams.code),"GET","",function(res){
            if(res.ret == 0)
            {
                var data = JSON.parse(res.data[0]);
                if(data.code == 200 && data.data)
                {
                    LayaCommon.getPostJson(encodeURIComponent(LOGIN_USER),encodeURIComponent("partner_id="+m_this.appId+"&secret="+m_this.appKey+"&access_token="+data.data.access_token),"GET","",function(info){
                        if(info.ret == 0)
                        {
                            var myData = JSON.parse(info.data);
                            if(myData.code == 200 && myData.data)
                            {
                                m_this.userID = myData.data.uuid;
                                m_this.userToken = myData.data.token;
                                callback && callback({"result":0,"spuid":myData.data.uuid});
                            } else
                            {
                                callback && callback({"result":-2,"desc":"login error"});
                            }
                        }else
                        {
                            callback && callback({"result":-2,"desc":"login error"});
                        }
                    });
                } else
                {
                    callback && callback({"result":-2,"desc":"login error"});
                }
            }

        })
    }

    __proto.pay = function (param, callback) {
        var m_this = this;
        console.log("壕鑫网络支付：" + JSON.stringify(param));

        LayaCommon.getPostJson(encodeURIComponent(PAY_ORDER),encodeURIComponent("token="+ m_this.userToken+"&uuid="+m_this.userID+"&channel="+m_this.otherKey+"&contentId="+m_this.loginKey+"&rechargeAmount="+param.data.amount+"&consumeAmount="+param.data.amount+"&exOrderNo="+param.data.orderId),"GET","",function(res){
            if(res.ret == 0)
            {
               var myData = JSON.parse(res.data[0]);
                if(myData.code == 200)
                {
                    window.location.href = PAY_SHOW+"?token="+ m_this.userToken+"&uuid="+m_this.userID+"&channel="+m_this.otherKey+"&contentId="+m_this.loginKey+"&exOrderNo="+param.data.orderId+"&subject="+param.data.goodsName+"&amount="+param.data.amount+"&action=PAY"+"&orderno="+myData.data;
                }else {
                    callback&&callback({"result":-4,"desc":"error"});
                }
            }
        });
    };

    __proto.init = function (param, callback) {
        console.log("壕鑫网络初始化：" + JSON.stringify(param));
        var m_this = this;
        m_this.appId = param.appId;
        m_this.appKey = param.appKey;
        m_this.payKey = param.payKey;
        m_this.loginKey = param.loginKey;
        m_this.otherKey = param.other;
        m_this.queryParams = param.queryParams;
        LayaCommon.loadScript(BTN_URL,function(){
            callback && callback({"result": 0, "desc": "OK"});
        })
    };
    return SPSdk;
})();